<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/4/17
 * Time: 19:38
 */

namespace App\Http\Classes;
use Illuminate\Support\Facades\DB;

class Bowen
{
    public $bowen_id = null;
    public function __construct($bowen_id)
    {
        $this->bowen_id = $bowen_id;
    }

    public static function getUserBowen($dataArray){
        if(isset($dataArray['uid'])){
            $bowen = DB::table('weibo_bowens');
            $bowen = $bowen->where('bowen_user_id','=',$dataArray['uid']);
            if(isset($dataArray['orderBy'])){
                if($dataArray['orderBy'] == 'time'){
                    if(isset($dataArray['order'])){
                        if($dataArray['order'] == 'desc'){
                            $bowen = $bowen->orderBy('bowen_posttime','desc');
                        }
                        else if($dataArray['order'] == 'asc'){
                            $bowen = $bowen->orderBy('bowen_posttime','asc');
                        }
                        else{
                            $bowen = $bowen->orderBy('bowen_posttime');
                        }
                    }
                    else{
                        $bowen = $bowen->orderBy('bowen_posttime');
                    }
                }
                else if($dataArray['orderBy'] == 'hot'){
                    if(isset($dataArray['order'])){
                        if($dataArray['order'] == 'desc'){
                            $bowen = $bowen->orderBy('bowen_like_count','desc');
                        }
                        else if($dataArray['order'] == 'asc'){
                            $bowen = $bowen->orderBy('bowen_like_count','asc');
                        }
                        else{
                            $bowen = $bowen->orderBy('bowen_like_count');
                        }
                    }
                    else{
                        $bowen = $bowen->orderBy('bowen_like_count');
                    }
                }
                else{
                    $bowen = $bowen->orderBy('bowen_posttime');
                }
            }
            else{
                $bowen = $bowen->orderBy('bowen_posttime');
            }

            if(isset($dataArray['start'])){
                $bowen = $bowen->skip($dataArray['start']);
            }

            if(isset($dataArray['size'])){
                $bowen = $bowen->take($dataArray['size']);
            }
            else{
                $bowen = $bowen->take(10);
            }
            $bowen = $bowen->get();
            $author = new Author($dataArray['uid']);
            $author = $author->bowenAuthor();
            $data = [];
            for ($i = 0; $i < count($bowen); $i++){
                $data[$i]['bowen'] = $bowen[$i];
                $data[$i]['author'] = $author;
                $data[$i]['repost'] = null;
                if($bowen[$i]->bowen_repost_id != null){
                    $repost = new Bowen($bowen[$i]->bowen_repost_id);
                    $data[$i]['repost'] = $repost->getRepost();
                }
            }
            return $data;
        }
        else{
            return null;
        }
    }

    public function getBowen(){
        $bowen = DB::table('weibo_bowens');
        $result = $bowen->where('bowen_id', '=', $this->bowen_id)->get();
        if($result){
            $data = [];
            $data['bowen'] = $result[0];
            $author = new Author($result[0]->bowen_user_id);
            $data['author'] = $author->bowenAuthor();
            $data['repost'] = null;
            if($result[0]->bowen_repost_id != null && $result[0]->bowen_repost_id != 'null'){
                $repost = new Bowen($result[0]->bowen_repost_id);
                $data['repost'] = $repost->getRepost();
            }
            return $data;
        }
        else{
            return null;
        }
    }

    public static function getAllBowen(){
        $bowen = DB::table('weibo_bowens');
        $bowen = $bowen->where('bowen_id','>',80)->get();
        $data = [];
        if($bowen){
            for ($i = 0; $i < count($bowen); $i++){
                $data[$i]['bowen'] = $bowen[$i];
                $author = new Author($bowen[$i]->bowen_user_id);
                $data[$i]['author'] = $author->bowenAuthor();
                $data[$i]['repost'] = null;
                if($bowen[$i]->bowen_repost_id != null){
                    $repost = new Bowen($bowen[$i]->bowen_repost_id);
                    $data[$i]['repost'] = $repost->getRepost();
                }
            }

            return $data;
        }
        else{
            return null;
        }
    }

    public static function pubBowen($dataArray){
        $bowen = DB::table('weibo_bowens');
        $result = $bowen->insertGetId([
            'bowen_content' => $dataArray['bowen_content'],
            'bowen_user_id' => $dataArray['bowen_user_id'],
            'bowen_img_count' => $dataArray['bowen_img_count'],
            'bowen_img_urls' => $dataArray['bowen_img_urls']
        ]);
        if($result){
            $data = [];
            $bowen = $bowen->where('bowen_id','=',$result)->get();
            if($bowen){
                $data['bowen'] = $bowen[0];
                $author = new Author($bowen[0]->bowen_user_id);
                $data['author'] = $author->bowenAuthor();
                $data['repost'] = null;
                if($bowen[0]->bowen_repost_id != null && $bowen[0]->bowen_repost_id != 'null'){
                    $repost = new Bowen($bowen[0]->bowen_repost_id);
                    $data['repost'] = $repost->getRepost();
                }
                return $data;
            }
            else{
                return null;
            }
        }
        else{
            return false;
        }
    }

    private function getRepost(){
        $bowen = DB::table('weibo_bowens');
        $result = $bowen->where('bowen_id','=',$this->bowen_id)->get();
        if($result){
            $data = [];
            $data['bowen'] = $result[0];
            $author = new Author($result[0]->bowen_user_id);
            $data['author'] = $author->bowenAuthor();
            return $data;
        }
        else{
            return null;
        }
    }

    public function likeBowen(){
        $bowen = DB::table('weibo_bowens');
        $result = $bowen->where('bowen_id', '=', $this->bowen_id)->increment('bowen_like_count');
        if($result){
            $bowen = $bowen->where('bowen_id', '=', $this->bowen_id)->select('bowen_like_count')->get();
            return $bowen[0];
        }
        else{
            return false;
        }
    }

    public static function getBowens($data){
        $bowen = DB::table('weibo_bowens');
        $count = $bowen->count();
        if(isset($data['orderBy']) && isset($data['order'])){
            if($data['orderBy'] == 'id'){
                if($data['order'] == 'desc'){
                    $bowen = $bowen -> orderBy('bowen_id','desc');
                }
                else{
                    $bowen = $bowen -> orderBy('bowen_id','asc');
                }
            }
            else if($data['orderBy'] == 'comment'){
                if($data['order'] == 'desc'){
                    $bowen = $bowen -> orderBy('bowen_comment_count','desc');
                }
                else{
                    $bowen = $bowen -> orderBy('bowen_comment_count','asc');
                }
            }
            else if($data['orderBy'] == 'like'){
                if($data['order'] == 'desc'){
                    $bowen = $bowen -> orderBy('bowen_like_count','desc');
                }
                else{
                    $bowen = $bowen -> orderBy('bowen_like_count','asc');
                }
            }
            else if($data['orderBy'] == 'time'){
                if($data['order'] == 'desc'){
                    $bowen = $bowen -> orderBy('bowen_posttime','desc');
                }
                else{
                    $bowen = $bowen -> orderBy('bowen_posttime','asc');
                }
            }
            else if($data['orderBy'] == 'author'){
                if($data['order'] == 'desc'){
                    $bowen = $bowen -> orderBy('bowen_user_id','desc');
                }
                else{
                    $bowen = $bowen -> orderBy('bowen_user_id','asc');
                }
            }
            else{
                $bowen = $bowen -> orderBy('bowen_id','asc');
            }
        }
        else{
            $bowen = $bowen -> orderBy('bowen_id','asc');
        }

        if(isset($data['index']) && isset($data['limit'])){
            $bowen -> skip(($data['index'] - 1) * $data['limit']) -> take($data['limit']);
        }
        else{
            $bowen -> skip(1) -> take(20);
        }

        $bowen = $bowen -> get();

        if($bowen){
            $result = [];
            $result['total'] = $count;
            $result['bowens'] = $bowen;
            return $result;
        }
        else{
            return null;
        }
    }

    public static function repost($dataArray){
        $bowen = DB::table('weibo_bowens');
        $result = $bowen->insertGetId([
            'bowen_content' => $dataArray['bowen_content'],
            'bowen_user_id' => $dataArray['bowen_user_id'],
            'bowen_repost_id' => $dataArray['repost_id']
        ]);
        if($result){
            //var_dump($bowen);
            //$count = $bowen;
            DB::table('weibo_bowens')->where('bowen_id','=',$dataArray['repost_id'])->increment('bowen_repost_count',1);
            //unset($count);
            //var_dump($count);
            $data = [];
            $result = $bowen->where('bowen_id','=',$result)->get();
            //var_dump($result);
            if($result){
                $data['bowen'] = $result[0];
                $author = new Author($result[0]->bowen_user_id);
                $data['author'] = $author->bowenAuthor();
                $data['repost'] = null;
                if($result[0]->bowen_repost_id != null && $result[0]->bowen_repost_id != 'null'){
                    $repost = new Bowen($result[0]->bowen_repost_id);
                    $data['repost'] = $repost->getRepost();
                }
                return $data;
            }
            else{
                return null;
            }
        }
        else{
            return false;
        }
    }
}